﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Controls;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.VisualStudio.TeamFoundation.WorkItemTracking;

namespace TeamReview.UI
{
	public partial class WIGrid : UserControl
	{
		private WorkItemResultGrid _grid = null;
	
		public event WorkItemEventHandler ReplayChangeRequested;
		public event WorkItemEventHandler ShowWorkItemRequested;

		public bool HasItems
		{
			get { return (_grid != null && _grid.Rows.Count > 0); }

		}


		public WIGrid()
		{
			InitializeComponent();
			Text = "Work Items";
			_grid = new WorkItemResultGrid();
            
			_grid.StretchLastColumn = true;
			_grid.Dock = DockStyle.Fill;
			_grid.AllowUserToAddRows = false;
			_grid.AllowUserToDeleteRows = false;
			_grid.AllowUserToOrderColumns = false;
			_grid.AllowUserToResizeColumns = true;
			_grid.AllowUserToResizeRows = true;
			_grid.ColumnHeadersVisible = true;
			_grid.MultiSelect = false;
			_grid.VirtualMode = true;
			_grid.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
			
			_grid.SelectionChanged += new System.EventHandler(ReplayChangedHandler);
			_grid.CellDoubleClick += new DataGridViewCellEventHandler(ShowWorkItemHandler);
			_grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

            DataGridViewCellStyle style = new DataGridViewCellStyle(_grid.AlternatingRowsDefaultCellStyle);
            style.BackColor = Color.LightYellow;
            _grid.AlternatingRowsDefaultCellStyle = style;

            System.Windows.Forms.DataGridViewCellStyle style2 = new System.Windows.Forms.DataGridViewCellStyle();
            style2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
            style2.BackColor = System.Drawing.SystemColors.Control;
            style2.ForeColor = System.Drawing.SystemColors.WindowText;
            style2.SelectionBackColor = System.Drawing.SystemColors.Highlight;
            style2.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
            style2.WrapMode = System.Windows.Forms.DataGridViewTriState.False;
            _grid.ColumnHeadersDefaultCellStyle = style2;
            DisplayFieldCollection Fields = new DisplayFieldCollection();
            Fields.Clear();
            Fields.Add(new DisplayField("ID", 25));
            Fields.Add(new DisplayField("State", 50));
            Fields.Add(new DisplayField("Assigned To", 100));
            Fields.Add(new DisplayField("Title", 100));
            Fields.Add(new DisplayField(Constants.CODE_DESCRIPTION_FIELD, 200));
            Fields.Add(new DisplayField(Constants.CODE_RESOLUTION_FIELD, 200));
            
            _grid.ResultOptions = new ResultOptions(Fields, new SortFieldCollection());
            _grid.AutoSize = true;
            _grid.ValueProvider = DependencyLocater.Instance.ValueProvider;
            toolTip1.SetToolTip(_grid, "Select a row to find the code for that row's Code Review Response. Double Click a row to open that row's Code Review Response.");
			Controls.Add(_grid);
		}



		

		public void LoadLinkedItems(int workItemId)
		{
			DependencyLocater.Instance.TFSService.LoadGridWithLinkedWorkItems(ref _grid, workItemId);

			if (_grid.Rows.Count > 0 && _grid.Columns.Count > 0)
			{
				_grid.Rows[0].Selected = true;
				_grid.Columns[4].HeaderText = "Description";
				_grid.Columns[5].HeaderText = "Suggested Resolution";
				ResizeColumns();
				_grid.Update();
			}

		}



		private void ResizeColumns()
		{
			int width = _grid.Columns[3].Width + _grid.Columns[4].Width + _grid.Columns[5].Width;
			int titleWidth = (width - (width % 5)) / 5;
			width -= titleWidth;
			int descWidth = (width - (width % 2)) / 2;
			_grid.Columns[3].Width = titleWidth;
			_grid.Columns[4].Width = descWidth;
			_grid.Columns[5].Width = (width - descWidth);
		}


		private int GetCurrentSelectedWorkItemId()
		{
			int workItemId = 0;
			if (_grid.CurrentRow == null || _grid.CurrentRow.Cells == null || _grid.CurrentRow.Cells.Count <= 0 || _grid.CurrentRow.Cells[0].Value == null)
				return workItemId;

			Int32.TryParse(_grid.CurrentRow.Cells[0].Value.ToString(), out workItemId);
			return workItemId;
		}


		private void ReplayChangedHandler(object sender, System.EventArgs args)
		{
			int workItemId = GetCurrentSelectedWorkItemId();
			if (workItemId <= 0)
				return;

			OnReplayChangeRequested(workItemId);
		}


		private void ShowWorkItemHandler(object sender, System.EventArgs args)
		{
			int workItemId = GetCurrentSelectedWorkItemId();
			if (workItemId <= 0)
				return;

			OnShowWorkItemRequested(workItemId);
		}


		protected virtual void OnShowWorkItemRequested(int workItemId)
		{
			if (ShowWorkItemRequested == null)
				return;

			WorkItemEventArgs args = new WorkItemEventArgs(workItemId);
			ShowWorkItemRequested(this, args);
		}


		protected virtual void OnReplayChangeRequested(int workItemId)
		{
			if (ReplayChangeRequested == null)
				return;

			WorkItemEventArgs args = new WorkItemEventArgs(workItemId);
			ReplayChangeRequested(this, args);
		}
	}
}
